package com.ruoyi.kettle.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.kettle.domain.KettleJob;
import com.ruoyi.kettle.domain.KettleTrans;
import com.ruoyi.kettle.service.IKettleJobService;
import com.ruoyi.kettle.service.IKettleTransService;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/api/kettle")
public class KettleApiController extends BaseController {
    @Autowired
    private IKettleJobService kettleJobService;
    @Autowired
    private IKettleTransService kettleTransService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysJobService jobService;


    /**
     * 查询作业调度列表
     */
    @RequiresPermissions("kettle:job:list")
    @PostMapping("/job/list")
    @ResponseBody
    public TableDataInfo list(KettleJob kettleJob)
    {
        startPage();
        List<KettleJob> list = kettleJobService.selectKettleJobList(kettleJob);
        return getDataTable(list);
    }
    /**
     * 查询转换列表
     */
    @RequiresPermissions("kettle:trans:list")
    @PostMapping("/trans/list")
    @ResponseBody
    public TableDataInfo list(KettleTrans kettleTrans)
    {
        startPage();
        List<KettleTrans> list = kettleTransService.selectKettleTransList(kettleTrans);
        return getDataTable(list);
    }
    /**
     * 导出作业调度列表
     */
    @RequiresPermissions("kettle:job:export")
    @Log(title = "作业调度", businessType = BusinessType.EXPORT)
    @PostMapping("/job/export")
    @ResponseBody
    public AjaxResult export(KettleJob kettleJob)
    {
        List<KettleJob> list = kettleJobService.selectKettleJobList(kettleJob);
        ExcelUtil<KettleJob> util = new ExcelUtil<KettleJob>(KettleJob.class);
        return util.exportExcel(list, "作业调度数据");
    }
    /**
     * 导出转换列表
     */
    @RequiresPermissions("kettle:trans:export")
    @Log(title = "转换", businessType = BusinessType.EXPORT)
    @PostMapping("/trans/export")
    @ResponseBody
    public AjaxResult export(KettleTrans kettleTrans)
    {
        List<KettleTrans> list = kettleTransService.selectKettleTransList(kettleTrans);
        ExcelUtil<KettleTrans> util = new ExcelUtil<KettleTrans>(KettleTrans.class);
        return util.exportExcel(list, "转换数据");
    }

    /**
     * 新增保存作业调度
     */
    @RequiresPermissions("kettle:job:add")
    @Log(title = "作业调度", businessType = BusinessType.INSERT)
    @PostMapping("/job/add")
    @ResponseBody
    public AjaxResult addSave(KettleJob kettleJob)
    {
        return kettleJobService.insertKettleJob(kettleJob);
    }
    /**
     * 新增保存转换
     */
    @RequiresPermissions("kettle:trans:add")
    @Log(title = "转换", businessType = BusinessType.INSERT)
    @PostMapping("/trans/add")
    @ResponseBody
    public AjaxResult addSave(KettleTrans kettleTrans)
    {
        return  kettleTransService.insertKettleTrans(kettleTrans) ;
    }
    /**
     * 修改保存作业调度
     */
    @RequiresPermissions("kettle:job:edit")
    @Log(title = "作业调度", businessType = BusinessType.UPDATE)
    @PostMapping("/job/edit")
    @ResponseBody
    public AjaxResult editSave(KettleJob kettleJob)
    {
        return toAjax(kettleJobService.updateKettleJob(kettleJob));
    }
    /**
     * 修改保存转换
     */
    @RequiresPermissions("kettle:trans:edit")
    @Log(title = "转换", businessType = BusinessType.UPDATE)
    @PostMapping("/trans/edit")
    @ResponseBody
    public AjaxResult editSave(KettleTrans kettleTrans)
    {
        return toAjax(kettleTransService.updateKettleTrans(kettleTrans));
    }


    /**
     * 删除作业调度
     */
    @RequiresPermissions("kettle:job:remove")
    @Log(title = "作业调度", businessType = BusinessType.DELETE)
    @PostMapping( "/job/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(kettleJobService.deleteKettleJobByIds(ids));
    }

    /**
     * 删除转换
     */
    @RequiresPermissions("kettle:trans:remove")
    @Log(title = "转换", businessType = BusinessType.DELETE)
    @PostMapping( "/trans/remove")
    @ResponseBody
    public AjaxResult removeTrans(String ids)
    {
        return toAjax(kettleTransService.deleteKettleTransByIds(ids));
    }

    @Log(title = "立即执行作业", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kettle:job:run")
    @PostMapping("/job/run")
    @ResponseBody
    public AjaxResult run(KettleJob job)
    {
        AjaxResult result = kettleJobService.run(job);
        return result;
    }
    @Log(title = "通过作业名称立即执行作业", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kettle:job:run")
    @PostMapping("/job/runByName")
    @ResponseBody
    public AjaxResult runJobByName(KettleJob job)
    {
        AjaxResult result = kettleJobService.runByName(job);
        return result;
    }
    /**
     * 转换立即执行一次
     */
    @Log(title = "立即执行转换", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kettle:trans:run")
    @PostMapping("/trans/run")
    @ResponseBody
    public AjaxResult runToQueue(KettleTrans trans)
    {
        AjaxResult result = kettleTransService.runToQueue(trans);
        return result;
    }
    /**
     * 通过转换名称 转换立即执行一次
     */
    @Log(title = "通过转换名称立即执行转换", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kettle:trans:run")
    @PostMapping("/trans/runByName")
    @ResponseBody
    public AjaxResult runTransByName(KettleTrans trans)
    {
        AjaxResult result = kettleTransService.runByName(trans);
        return result;
    }
}
